#include <stdio.h>
#include <stdlib.h>
int *test()
{
    int *p = (int *)malloc(sizeof(int) * 10);
    if (p == NULL)
    {
        printf("malloc error!\n");
        return 0; // 申请堆空间的标准写法
    }
    for (int i = 0; i < 10; i++)
    {
        *(p + i) = i + 1;
        printf("%d ", *(p + i));
    }
    printf("\n");
    free(p);
    p = NULL;
    return p;
}
void Malloc()
{
    int **p = (int **)malloc(sizeof(int *) * 2); // 用指针数组去构建二维数组
    for (int i = 0; i < 2; i++)
    {
        p[i] = (int *)malloc(sizeof(int) * 3);
    }
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            p[i][j] = i * 3 + j + 1;
            printf("%d ", p[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 2; i++)
    {
        free(p[i]);
    }
    free(p);
    p = NULL;
}
void Malloc2()
{
    int(*p)[3] = (int(*)[3])malloc(sizeof(int) * 2 * 3);
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            p[i][j] = i * 3 + j + 1;
            printf("%d", p[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    // int *p = test();
    // if (p != NULL)
    // {
    //     free(p);
    // }
    // Malloc();
    int *p = (int *)calloc(3, sizeof(int));

    for (int i = 0; i < 3; i++)
    {
        p[i] = i + 1;
        printf("%d", p[i]);
    }
    printf("\n");
    int *p1 = relloc(NULL, 5 * sizeof(int));
    for (int i = 0; i < 5; i++)
    {
        p[i] = i + 1;
        printf("%d", p[i]);
    }
    printf("\n");
    free(p);
    for (int i = 0; i < 5; i++)
    {
        p[i] = i + 1;
        printf("%d", p[i]);
    }
    prinf("\n");
    printf("%x,%x\n", p, p1);

    return 0;
}